package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.R;
import io.renren.modules.app.dao.ReportBankaccDao;
import io.renren.modules.app.dao.UserDao;
import io.renren.modules.app.entity.UserEntity;
import io.renren.modules.app.service.AppReportBankaccService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.*;


@Service("reportBankaccWebService")
public class AppReportBankaccServiceImpl extends ServiceImpl<UserDao, UserEntity> implements AppReportBankaccService {

    @Autowired
    private ReportBankaccDao reportBankaccDao;

    //银行对账日记账
    @Override
    public R findPage(Map<String, Object> data) {

        String pkorg = data.get("pkorg").toString();
        String begindate = data.get("begindate").toString();
        String enddate = data.get("enddate").toString();
        int cur = Integer.parseInt(data.get("currPage").toString());//当前页
        int size = Integer.parseInt(data.get("pageSize").toString());//一页的记录数
        int anum;//每页的第一条数据的行号
        int wnum = cur * size; //每页的最后一条数据的行号
        if (cur == 1) {
            anum = 1;
        } else {
            anum = (cur - 1) * size + 1;
        }
        List<Map<String, Object>> list = reportBankaccDao.reportBankaccWebService(anum, wnum, pkorg, begindate, enddate);
        return R.ok().put("data", list);
    }

    //银行记账日记账的详情
    @Override
    public R bankDepositAcWebService(Map<String, Object> data) {

        String pkorg = data.get("pkorg").toString();
        if (pkorg == null || pkorg.length() == 0) {

            return R.error().put("data", "请选择公司");
        }
        String accnum = data.get("accnum").toString();
        if (accnum == null || accnum.length() == 0) {

            return R.error().put("data", "请选择银行账号");
        }
        String begindate = data.get("begindate").toString();
        if (begindate == null || begindate.length() == 0) {

            return R.error().put("data", "请选择开始时间");
        }
        String enddate = data.get("enddate").toString();
        if (enddate == null || enddate.length() == 0) {

            return R.error().put("data", "请选择结束时间时间");
        }
        int cur = Integer.parseInt(data.get("currPage").toString());//当前页
        int size = Integer.parseInt(data.get("pageSize").toString());//一页的记录数

        int anum;//每页的第一条数据的行号
        int wnum = cur * size; //每页的最后一条数据的行号
        if (cur == 1) {
            anum = 1;
        } else {
            anum = (cur - 1) * size + 1;
        }


        //获取该银行账号的期初值
        Map<String, Object> listini = reportBankaccDao.reportBankaChq(anum, wnum, pkorg, accnum, begindate, enddate);
        BigDecimal initnum = (BigDecimal) listini.get("LOCAL_AMOUNT");

        List<Map<String, Object>> list = reportBankaccDao.reportBankaDetail(anum, wnum, pkorg, accnum, begindate, enddate);


        List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
        BigDecimal qichu = initnum;
        System.out.println("====ccc===" + qichu);
        BigDecimal yuer = new BigDecimal("0");
        int i = 0;

        for (Map<String, Object> m : list) {
            Map<String, Object> newmap = new HashMap<String, Object>();

            for (String k : m.keySet()) {

                newmap.put(k, m.get(k));
                System.out.println("==bbbbbbb=" + k + " : " + m.get(k));

            }
            ++i;
            BigDecimal shouru = (BigDecimal) m.get("RECMONEY");
            BigDecimal zhichu = (BigDecimal) m.get("PAYMONEY");
            if (i == 1) {

                newmap.put("BEGINNING", qichu);

                yuer = qichu.add(shouru).subtract(zhichu);
                newmap.put("BALANCE", yuer);
                listMap.add(newmap);

                yuer = qichu;
            } else {
                newmap.put("BALANCE", yuer);

                qichu = yuer.add(zhichu).subtract(shouru);
                newmap.put("BEGINNING", qichu);

                listMap.add(newmap);
                yuer = qichu;
            }

        }

        return R.ok().put("data", listMap);
    }

    //银行记账日记账的详情合计
    @Override
    public R reportBankaDetailzj(Map<String, Object> data) {

        try {
            String pkorg = data.get("pkorg").toString();
            if (pkorg == null || pkorg.length() == 0) {

                return R.error().put("data", "请选择公司");
            }
            String accnum = data.get("accnum").toString();
            if (accnum == null || accnum.length() == 0) {

                return R.error().put("data", "请选择银行账号");
            }
            String begindate = data.get("begindate").toString();

            if (begindate == null || begindate.length() == 0) {

                return R.error().put("data", "请选择开始时间");
            }
            String enddate = data.get("enddate").toString();

            if (enddate == null || enddate.length() == 0) {

                return R.error().put("data", "请选择结束时间时间");
            }

            Map<String, Object> list = reportBankaccDao.reportBankaDetailzj(pkorg, accnum, begindate, enddate);

            for (String k : list.keySet()) {


                System.out.println("==bbbbbbb=" + k + " : " + list.get(k));

            }

            //期初值
            Map<String, Object> listini = reportBankaccDao.reportBankaChq(1, 2, pkorg, accnum, begindate, enddate);
            BigDecimal initnum_s = (BigDecimal) listini.get("LOCAL_AMOUNT");//最新时间的期初值

            if (initnum_s.equals("")) {
                System.out.println("=========meiyou=");
            }
            BigDecimal zshouru = (BigDecimal) list.get("RECMONEY");

            BigDecimal zzhichu = (BigDecimal) list.get("PAYMONEY");


            BigDecimal shouru = new BigDecimal(0);
            BigDecimal zhichu = new BigDecimal(0);
            List<Map<String, Object>> list_s = reportBankaccDao.reportBankaDetail(1, 2, pkorg, accnum, begindate, enddate);
            for (Map<String, Object> m : list_s) {


                for (String k : m.keySet()) {

                     shouru = (BigDecimal) m.get("RECMONEY");
                     zhichu = (BigDecimal) m.get("PAYMONEY");

                }

            }

            BigDecimal initnum = initnum_s.add(zzhichu).subtract(zshouru);//期初
            BigDecimal BALANCE = initnum.add(shouru).subtract(zhichu);//余额
            list.put("ZBEGINNING", initnum); //期初
            list.put("ZBALANCE", BALANCE); //余额

            return R.ok().put("data", list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().put("data", "获取数据异常");
        }
    }


    @Override
    public R reportBankaDetailSub(Map<String, Object> data) {

        String pkorg = data.get("pkorg").toString();
        String accnum = data.get("accnum").toString();
        String begindate = data.get("begindate").toString();
        String enddate = data.get("enddate").toString();
        int cur = Integer.parseInt(data.get("currPage").toString());
        int size = Integer.parseInt(data.get("pageSize").toString());

        int anum;

        if (cur == 1) {
            anum = 1;
        } else {
            anum = (cur - 1) * size;
        }
        int wnum = cur * size;

        Map<String, Object> list = reportBankaccDao.reportBankaDetailSub(anum, wnum, pkorg, accnum, begindate, enddate);

        return R.ok().put("data", list);
    }

    @Override
    public R bankzj(String pkorg, String begindate, String enddate) {

        Map<String, Object> list = reportBankaccDao.reportBankzjWebService(pkorg, begindate, enddate);

        return R.ok().put("data", list);
    }

    //财务组织查询
    @Override
    public R orgorgsWebService(String pkorg) {

        List<Map<String, Object>> list = reportBankaccDao.orgorgsWebService(pkorg);

        return R.ok().put("data", list);
    }


    //财务组织全部查询
    @Override
    public R orgorgsAll(String pkorg) {

        List<Map<String, Object>> list = reportBankaccDao.orgorgsAll(pkorg);

        return R.ok().put("data", list);
    }

    //财务组织根据名称模糊查询
    @Override
    public R selectOrgorgsWebService(String name, String pkorg) {

        List<Map<String, Object>> list = reportBankaccDao.selectOrgorgsWebService(name, pkorg);

        return R.ok().put("data", list);
    }

}
